package cn.study.luckyadapter.controller;

import cn.study.common.annotation.ResponseResult;
import cn.study.luckyclient.api.IUserService;
import cn.study.luckyclient.dto.UserRegisterCmd;
import cn.study.luckyclient.dto.UserUpdateCmd;
import cn.study.luckyclient.dto.data.UserVo;
import cn.study.luckyclient.dto.query.UserLoginQuery;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@Slf4j
@ResponseResult
@AllArgsConstructor
@RequestMapping("/v1/user")
public class UserController {
    private final IUserService userService;
    @PostMapping("/register")
    public UserVo register(@Validated @RequestBody UserRegisterCmd userRegisterCmd){
           return userService.register(userRegisterCmd);
    }
    @GetMapping("/login")
    public String login(@Validated @RequestBody UserLoginQuery userLoginQuery, HttpServletResponse response){
        String login = userService.login(userLoginQuery);
        response.setHeader("Authorization",login);
        return  login;
    }
    @PutMapping("/update")
    public UserVo update(@Validated @RequestBody UserUpdateCmd cmd) {
       return userService.update(cmd);
    }
    @GetMapping("/one/{id}")
    public UserVo one(@Validated @PathVariable("id") Long id){
        return userService.one(id);
    }
}
